package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 大攻略的分类 controller资源
 */

@Controller
@RequestMapping("/strategyCatalog")
public class StrategyCatalogController {

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyService strategyService;


    /**
     * 分页及高查
     * @param qo : 关键字
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyCatalogQueryObject qo, Model model){
        qo.setOrderBy("sc.sequence asc");
        model.addAttribute("pageInfo",strategyCatalogService.query(qo));
        model.addAttribute("strategies",strategyService.listAll());
        return "strategyCatalog/list";
    }

    /**
     * 新增与保存
     * @param strategyCatalog
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(StrategyCatalog strategyCatalog) {
        JsonResult jsonResult = new JsonResult();

        try {
            if (strategyCatalog.getId() == null) {
                strategyCatalogService.save(strategyCatalog);
            }else{
                strategyCatalogService.update(strategyCatalog);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

    /**
     * 根据大攻略的id 查询该攻略的分类信息
     * @param strategyId : 大攻略id
     * @return
     */
    @RequestMapping("/getByStrategyId")
    @ResponseBody
    public List getByStrategyId(Long strategyId) {
        return strategyCatalogService.getByStrategyId(strategyId);
    }


}
